﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace ElfTyping.General
{
    public class DictionaryManager
    {
        string[] dictionaryFileNames = {
                                           "english-words.10",
                                           "english-words.20",
                                           "english-words.35",
                                           "english-words.40",
                                           "english-words.50",
                                           "english-words.55"
                                       };

        Random random = new Random();
        bool HasLoaded = false;
        Dictionary<int, List<string>> AlphaWordsByLength;
        Dictionary<int, List<string>> AllWordsByLength;

        private DictionaryManager() {}

        private static DictionaryManager _singleton = null;
        public static DictionaryManager GetManager()
        {
            if (_singleton == null)
            {
                _singleton = new DictionaryManager();
            }

            return _singleton;
        }

        public void LoadAllDictionaries()
        {
            if (HasLoaded)
            {
                return;
            }

            AlphaWordsByLength = new Dictionary<int, List<string>>();
            AllWordsByLength = new Dictionary<int, List<string>>();

            foreach (string dictionaryFileName in dictionaryFileNames)
            {
                LoadDictionary(Path.Combine(@"Content\Dictionary\", dictionaryFileName));
            }

            HasLoaded = true;
        }

        Regex alphaCharactersRegex = new Regex("^[a-zA-Z]+$"); // just alpha characters
        public void LoadDictionary(string dictionaryFileName)
        {            
            string[] allWords = File.ReadAllLines(dictionaryFileName);
            
            foreach (string word in allWords)
            {
                if (alphaCharactersRegex.IsMatch(word))
                {
                    InsertWordIntoLengthDictionary(word, AlphaWordsByLength);
                }
                InsertWordIntoLengthDictionary(word, AllWordsByLength);
            }
        }

        private void InsertWordIntoLengthDictionary(string word, Dictionary<int, List<string>> dictionary)
        {
            if (!dictionary.ContainsKey(word.Length))
            {
                List<string> lengthWordList = new List<string>() { word };
                dictionary.Add(word.Length, lengthWordList);
            }
            else
            {
                dictionary[word.Length].Add(word);
            }
        }


        public string GetRandomWord(int length, bool includeNonAlpha)
        {
            Dictionary<int, List<string>> dictionary = includeNonAlpha ? AllWordsByLength : AlphaWordsByLength;
            List<string> wordsWithLength = dictionary[length];
            return wordsWithLength[random.Next(wordsWithLength.Count)].ToUpper();
        }
    }
}
